import { Component, EventEmitter, Input, Output } from '@angular/core';
import { EditState } from '../../core';

@Component({
  // eslint-disable-next-line @angular-eslint/component-selector
  selector: 'j:tbuttons',
  templateUrl: './tree-form-buttons.component.html',
  styleUrls: ['./tree-form-buttons.component.less'],
})
export class TreeFormButtonsComponent {
  //编辑状态
  @Input()
  editState: EditState = EditState.Empty;

  //按钮是否显示
  @Input()
  saveBtnVisible: boolean = true;
  @Input()
  giveupBtnVisible: boolean = true;
  @Input()
  newBtnVisible: boolean = true;
  @Input()
  deleteBtnVisible: boolean = true;

  //按钮是否可用处理方法
  @Input()
  saveBtnDisabledFn: () => boolean = () =>
    this.editState == EditState.Empty ||
    this.editState == EditState.Creating ||
    this.editState == EditState.View ||
    this.editState == EditState.Appoving ||
    this.editState == EditState.Appoved;
  @Input()
  giveupBtnDisabledFn: () => boolean = () =>
    this.editState == EditState.Empty ||
    this.editState == EditState.Creating ||
    this.editState == EditState.View ||
    this.editState == EditState.Appoving ||
    this.editState == EditState.Appoved;
  @Input()
  newBtnDisabledFn: () => boolean = () => false;
  @Input()
  deleteBtnDisabledFn: () => boolean = () =>
    this.editState == EditState.Empty ||
    this.editState == EditState.Creating ||
    this.editState == EditState.Appoving ||
    this.editState == EditState.Appoved;

  //按钮是否可用属性
  get saveBtnDisabled() {
    return this.saveBtnDisabledFn();
  }
  get giveupBtnDisabled() {
    return this.giveupBtnDisabledFn();
  }
  get newBtnDisabled() {
    return this.newBtnDisabledFn();
  }
  get deleteBtnDisabled() {
    return this.deleteBtnDisabledFn();
  }

  //按钮点击事件
  @Output() readonly saveBtnClick = new EventEmitter();
  @Output() readonly giveupBtnClick = new EventEmitter();
  @Output() readonly newBtnClick = new EventEmitter();
  @Output() readonly deleteBtnClick = new EventEmitter();

  OnSaveBtnClick() {
    this.saveBtnClick.emit();
  }
  OnGiveupBtnClick() {
    this.giveupBtnClick.emit();
  }
  OnNewBtnClick() {
    this.newBtnClick.emit();
  }
  OnDeleteBtnClick() {
    this.deleteBtnClick.emit();
  }

  @Output() readonly searchBtnClick = new EventEmitter();
  code: string;
  OnSearchBtnClick() {
    this.searchBtnClick.emit(this.code);
    this.code = undefined;
  }

  @Input()
  searchPlaceholder: string = '';
}
